<template>
  <el-container class="add-user-container">
    <el-header class="page-header">
      <div class="header-content">
        <el-button @click="handleBack">
          <el-icon><ArrowLeft /></el-icon> 返回列表
        </el-button>
        <h2 class="page-title">添加用户</h2>
      </div>
    </el-header>

    <el-main class="page-content">
      <el-card class="add-user-card">
        <el-form
          ref="userFormRef"
          :model="userForm"
          :rules="formRules"
          label-width="100px"
          class="user-form"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="userForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" type="password" show-password></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="userForm.email" type="email"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="userForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="role">
            <el-select v-model="userForm.role" placeholder="请选择角色">
              <el-option label="普通用户" value="User"></el-option>
              <el-option label="管理员" value="Admin"></el-option>
              <el-option label="超级管理员" value="SuperAdmin"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="form-actions">
            <el-button @click="resetForm">重置</el-button>
            <el-button type="primary" @click="handleFormSubmit">提交</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { ArrowLeft } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
// 导入验证规则
import { userFormRules } from '../utils/validationRules';

const router = useRouter();
const userFormRef = ref(null);

// 用户表单数据
const userForm = ref({
  username: '',
  name: '',
  password: '',
  email: '',
  phone: '',
  role: 'User'
});

// 表单验证规则
const formRules = userFormRules;

// 返回用户列表
const handleBack = () => {
  router.push('/users/list');
};
//重置表单
const resetForm = () => {
  userFormRef.value.resetFields();
};
// 提交表单
const handleFormSubmit = async () => {
  // 验证表单
  const valid = await userFormRef.value.validate();
  if (!valid) return;

  // 模拟API请求
  try {
    // 这里应该是实际的API调用
    console.log('提交用户数据:', userForm.value);
    ElMessage.success('用户添加成功');
    router.push('/users');
  } catch (error) {
    ElMessage.error('用户添加失败: ' + error.message);
  }
};
</script>

<style scoped>
.add-user-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-header {
  background-color: #fff;
  padding: 0 20px;
  border-bottom: 1px solid #e4e7ed;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
}

.page-title {
  margin: 0;
  font-size: 1.5rem;
  color: #303133;
}

.page-content {
  flex: 1;
  padding: 20px;
  background-color: #f5f7fa;
}

.add-user-card {
  max-width: 600px;
  margin: 0 auto;
}

.user-form {
  margin-top: 20px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}
</style>